package kevin.framework.basic.common.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.thread.LangThreadHodler;
import kevin.framework.basic.common.utils.IControlCommon;

/**
 * @ClassName: ClientButton
 * @Description: 客户端toolbar工具栏按钮类
 * @author kevin.huang
 * @date 2014-12-11 上午11:09:38
 */
public class ClientButton {
	private String methodsObject = "toolbarMethods";// 客户端用于定义按钮事件的window下的对象，默认值为

	public String getMethodsObject() {
		return methodsObject;
	}

	public void setMethodsObject(String methodsObject) {
		this.methodsObject = methodsObject;
	}

	private String id;
	private String text;
	private String handler;// 事件名称
	private String iconCls; // 样式
	private Map<String, String> params = new HashMap<String, String>();// 附加参数
	private boolean disabled = false;// 禁用，启用
	private String cmd;// 权限标识
	private String color; // 颜色

	private List<ClientButton> childrens = new ArrayList<ClientButton>();

	public List<ClientButton> getChildrens() {
		return childrens;
	}

	public void setChildren(ClientButton children) {
		this.childrens.add(children);
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	/** 是否需要显示该按钮 **/
	private boolean visualable = true;// 是否可见，用于服务端生成json

	public String getCmd() {
		return cmd == null ? "" : cmd;
	}

	public void setCmd(String cmd) {
		params.put("cmd", cmd);
		this.cmd = cmd;
	}

	/*
	 * public String toJSONString() { if (this.visualable) { return
	 * this.toJSONObject().toJSONString(); } else { return null; } }
	 */

	public JSONObject toJSONObject() {
		return this.toJSONObject(null, null, null);
	}

	public <T> JSONObject toJSONObject(HttpServletRequest request, IControlCommon<T> controll, T obj) {
		JSONObject jo = new JSONObject();

		if (this.childrens.size() > 0) {

			JSONArray array = new JSONArray();

			for (ClientButton c : this.childrens) {
				array.add(c.toJSONObject(request, controll, obj));
			}

			jo.put("childrens", array);
		} else {
			if (request != null) {
				// 权限判断
				if (controll.hasDataPrivilage(obj, request, this.getCmd())) {
					this.setDisabled(false);
					controll.buttonCreateHanlder(obj, this);
					this.setParams("privilage", "1");
				} else {
					this.setParams("privilage", "0");
					this.setDisabled(true);
				}
			}
		}

		jo.put("id", this.id == null ? "" : this.id);
		jo.put("text", this.text == null ? "" : this.text);
		jo.put("click", this.handler == null ? "" : this.handler);
		jo.put("iconCls", this.iconCls == null ? "" : this.iconCls);
		jo.put("disabled", this.disabled);
		jo.put("cmd", this.cmd == null ? "" : this.cmd);
		jo.put("visualable", this.visualable);
		jo.put("color", this.color);
		jo.put("methodsObject", this.methodsObject);

		Set<String> keys = this.params.keySet();
		JSONObject p = new JSONObject();
		for (String key : keys) {
			p.put(key, this.params.get(key));
		}
		jo.put("params", p);

		return jo;
	}

	public Map<String, String> getparams() {
		return this.params;
	}

	public String getId() {
		return id == null ? "" : id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getHandler() {
		return handler;
	}

	public void setHandler(String eventName) {
		this.handler = eventName;
	}

	public String getIconCls() {
		return iconCls == null ? "" : iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isVisualable() {
		return visualable;
	}

	public void setVisualable(boolean visualable) {
		this.visualable = visualable;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _btnText      显示的文字
	 * @param _btnEventName 脚本事件
	 * @param privilageCmd  权限配置里面的cmd
	 **/
	public ClientButton(String _btnText, String _btnEventName, String cmd) {
		this.text = _btnText;
		this.handler = _btnEventName;
		params.put("cmd", cmd);
		this.cmd = cmd;
		this.visualable = true;
		this.disabled = false;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _id      id
	 * @param _btnText 显示的文字
	 * @param _btnText 脚本事件
	 * @param cmd      按钮的权限标识符
	 * @param iconCls  图标样式
	 **/
	public ClientButton(String _id, String _btnText, String _btnEventName, String cmd, String iconCls) {
		this.id = _id;
		this.text = _btnText;
		this.handler = _btnEventName;
		params.put("cmd", cmd);
		this.cmd = cmd;
		this.visualable = true;
		this.disabled = false;
		this.iconCls = iconCls;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _id      id
	 * @param _btnText 显示的文字
	 * @param _btnText 脚本事件
	 * @param cmd      按钮的权限标识符
	 * @param iconCls  图标样式
	 **/
	public ClientButton(String _id, String _btnText, String _btnEventName, String cmd, String iconCls,
			boolean disabled) {
		this.id = _id;
		this.text = _btnText;
		this.handler = _btnEventName;
		params.put("cmd", cmd);
		this.cmd = cmd;
		this.visualable = true;
		this.disabled = disabled;
		this.iconCls = iconCls;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _id      id
	 * @param _btnText 显示的文字
	 * @param _btnText 脚本事件
	 * @param cmd      按钮的权限标识符
	 **/
	public ClientButton(String _id, String _btnText, String _btnEventName, String cmd) {
		this.id = _id;
		this.text = _btnText;
		this.handler = _btnEventName;
		params.put("cmd", cmd);
		this.cmd = cmd;
		this.visualable = true;
		this.disabled = false;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _btnText 显示的文字
	 * @param _btnText 脚本事件
	 * @param _disable 当该行数据进入某个状态时，该按钮是否禁用
	 **/
	public ClientButton(String _btnText, String _btnEventName, boolean _disable) {
		this.text = _btnText;
		this.handler = _btnEventName;
		params.put("cmd", "");
		this.visualable = true;
		this.disabled = false;
	}

	/**
	 * 如果有参数请setParams
	 * 
	 * @param _btnText 显示的文字
	 * @param _btnText 脚本事件
	 **/
	public ClientButton(String _btnText, String _btnEventName) {
		this.text = _btnText;
		this.handler = _btnEventName;
		this.visualable = true;
		params.put("cmd", "");
		this.disabled = false;
	}

	public ClientButton copy() {
		ClientButton nb = new ClientButton(this.id, this.text, this.handler, this.cmd);
		nb.disabled = this.disabled;
		nb.iconCls = this.iconCls;
		nb.color = this.color;
		nb.methodsObject = this.methodsObject;
		nb.childrens = this.childrens;
		Map<String, String> prs = new HashMap<String, String>();
		if (this.params.size() > 0) {
			for (Entry<String, String> entry : this.params.entrySet()) {
				prs.put(entry.getKey(), entry.getValue());
			}
		}
		nb.params = prs;
		nb.visualable = this.visualable;
		return nb;
	}

	/**
	 * 设置参数
	 ***/
	public void setParams(String pName, String val) {
		params.put(pName, val);
	}

	/**
	 * @Title: getDefaultAddBtn @Description: 获取默认的新增按钮 @return @throws
	 */
	public static ClientButton getDefaultAddBtn() {
		
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_add");
		return new ClientButton("btn_add", txt, "addFn", "add");
	}

	/**
	 * @Title: getDefaultAddBtn @Description: 获取默认的新增按钮 @return @throws
	 */
	public static ClientButton getDefaultAddBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_add");
		return new ClientButton("btn_add", txt, "addFn", "add", iconCls);
	}

	/**
	 * @Title: getDefaultDelBtn @Description: 获取默认的删除按钮 @return @throws
	 */
	public static ClientButton getDefaultDelBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_delete");
		return new ClientButton("btn_del", txt, "deleteFn", "delete");
	}

	/**
	 * @Title: getDefaultDelBtn @Description: 获取默认的删除按钮 @return @throws
	 */
	public static ClientButton getDefaultDelBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_delete");
		return new ClientButton("btn_del", txt, "deleteFn", "delete", iconCls);
	}

	/**
	 * @Title: getDefaultUpdateBtn @Description: 获取默认的更新按钮 @return @throws
	 */
	public static ClientButton getDefaultUpdateBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_edit");
		return new ClientButton("btn_update", txt, "updateFn", "update");
	}

	/**
	 * @Title: getDefaultUpdateBtn @Description: 获取默认的更新按钮 @return @throws
	 */
	public static ClientButton getDefaultUpdateBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_edit");
		return new ClientButton("btn_update", txt, "updateFn", "update", iconCls);
	}

	/**
	 * @Title: getDefaultQueryBtn @Description: 获取默认的查询按钮 @return @throws
	 */
	public static ClientButton getDefaultQueryBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_query");
		return new ClientButton("btn_query", txt, "queryFn", "query");
	}

	/**
	 * @Title: getDefaultQueryBtn @Description: 获取默认的查询按钮 @return @throws
	 */
	public static ClientButton getDefaultQueryBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_query");
		return new ClientButton("btn_query", txt, "queryFn", "query", iconCls);
	}

	/**
	 * @Title: getDefaultStatusBtn @Description: 获取默认的状态按钮 @return @throws
	 */
	public static ClientButton getDefaultStatusBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_disable");
		return new ClientButton("btn_status", txt, "statusFn", "update", "update");
	}

	/**
	 * @Title: getDefaultStatusBtn @Description: 获取默认的状态按钮 @return @throws
	 */
	public static ClientButton getDefaultStatusBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_disable");
		return new ClientButton("btn_status", txt, "statusFn", "status", iconCls);
	}

	/**
	 * @Title: getDefaultUploadXlsBtn @Description: 获取默认的上传xls的按钮 @return @throws
	 */
	public static ClientButton getDefaultUploadXlsBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_uploadData");
		return new ClientButton("btn_xlsupload", txt, "xlsuploadFn", "xlsupload");
	}

	/**
	 * @Title: getDefaultUploadXlsBtn @Description: 获取默认的上传xls的按钮 @return @throws
	 */
	public static ClientButton getDefaultUploadXlsBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_uploadData");
		return new ClientButton("btn_xlsupload", txt, "xlsuploadFn", "xlsupload", iconCls);
	}

	/**
	 * @Title: getDefaultDownXlsBtn @Description: 获取默认的导出xls的按钮 @return @throws
	 */
	public static ClientButton getDefaultDownXlsBtn() {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_downloadData");
		return new ClientButton("btn_xlsdown", txt, "xlsdownFn", "xlsdown");
	}

	/**
	 * @Title: getDefaultDownXlsBtn @Description: 获取默认的导出xls的按钮 @return @throws
	 */
	public static ClientButton getDefaultDownXlsBtn(String iconCls) {
		Map<String,String>  lang = LangThreadHodler.getMap();
		String txt = lang.get("common_btn_downloadData");
		return new ClientButton("btn_xlsdown", txt, "xlsdownFn", "xlsdown", iconCls);
	}

	/**
	 * 获取增删除改查默认按钮
	 * 
	 * @return
	 */
	public static List<ClientButton> getCurdDefaultBtnList() {
		List<ClientButton> clientBtnList = new ArrayList<ClientButton>();
		// 添加工具栏按钮
		clientBtnList.add(ClientButton.getDefaultAddBtn());
		clientBtnList.add(ClientButton.getDefaultDelBtn());
		clientBtnList.add(ClientButton.getDefaultUpdateBtn());
		clientBtnList.add(ClientButton.getDefaultQueryBtn());
		return clientBtnList;
	}
}